package com.buding.fx.dao.impl;

import com.buding.fx.dao.TagDao;
import com.buding.fx.dao.TagGroupDao;
import com.buding.fx.entity.Tag;
import com.buding.fx.entity.TagGroup;
import com.buding.fx.utils.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;
import java.util.List;

/**
 * @author mingdongwei-jwk
 * @date 2022/10/11 16:32
 * @class com.buding.fx.dao.impl
 * @Description
 */
public class TagDaoImpl implements TagDao {
    @Override
    public List<Tag> selectAll() {
        List<Tag> list = new ArrayList<>();
        SqlSession session = null;
        try {
            session = MyBatisUtil.getSqlSession();
            TagDao mapper = session.getMapper(TagDao.class);
            list = mapper.selectAll();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (session!=null){
                session.close();
            }
        }
        return list;
    }

    @Override
    public int delete(Long id) {
        int r = 0;
        SqlSession session = null;
        try {
            session = MyBatisUtil.getSqlSession();
            TagDao mapper = session.getMapper(TagDao.class);
            r = mapper.delete(id);
            session.commit();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (session!=null){
                session.close();
            }
        }
        return r;
    }

    @Override
    public int update(Tag tag) {
        int r = 0;
        SqlSession session = null;
        try {
            session = MyBatisUtil.getSqlSession();
            TagDao mapper = session.getMapper(TagDao.class);
            r = mapper.update(tag);
            session.commit();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (session!=null){
                session.close();
            }
        }
        return r;
    }

    @Override
    public int insert(Tag tag) {
        int r = 0;
        SqlSession session = null;
        try {
            session = MyBatisUtil.getSqlSession();
            TagDao mapper = session.getMapper(TagDao.class);
            r = mapper.insert(tag);
            session.commit();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (session!=null){
                session.close();
            }
        }
        return r;
    }
}
